package com.ym.medical.service.impl;

import com.ym.core.utils.tkmybatis.BaseSVImpl;

import com.ym.medical.dao.ClinicManualDao;
import com.ym.medical.domain.entity.ClinicManualEntity;
import com.ym.medical.domain.param.ClinicManualParam;
import com.ym.medical.domain.vo.ClinicManualVO;
import com.ym.medical.service.IClinicManualSV;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ComputerName:LAPTOP-6GI4HOI6
 * Author:625
 * Date: 2019-05-24
 * Time: 14:56:46
 * 服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ClinicManualSVImpl extends BaseSVImpl<ClinicManualEntity, ClinicManualDao> implements IClinicManualSV {

	@Override
	public List<ClinicManualVO> listCustomer(ClinicManualParam clinicManualModel) {
		List<ClinicManualVO> clinicManualVOS = mapper.selectVOByModel(clinicManualModel);
		if (!clinicManualVOS.isEmpty()) {
			for (ClinicManualVO clinicManual : clinicManualVOS) {
				String imgStr = getFirstImgStr(clinicManual.getContent());
				if (imgStr != null) {
					if (imgStr.indexOf("images.yamimap.com") > -1) {
						clinicManual.setImg(imgStr + "?imageView2/2/w/68/h/51");
					} else {
						clinicManual.setImg(imgStr);
					}
				}
				clinicManual.setContent(null);
			}
		}
		return clinicManualVOS;
	}

	@Override
	public ClinicManualEntity findById(Long articleId, int width) {
		ClinicManualEntity clinicManual = this.selectById(articleId);
		String content = clinicManual.getContent();
		content = content.replaceAll("width=\"[^\"]*\"", "");
		String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
		Pattern p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
		Matcher m_image = p_image.matcher(content);
		String imgStr = null;
		String imgSrc = null;
		String reg1 = "src\\s*=\\s*\"?(.*?)(\"|>|\\s+)";
		while (m_image.find()) {
			// 得到<img />数据
			imgStr = m_image.group();
			// 匹配<img>中的src数据
			Matcher m1 = Pattern.compile(reg1).matcher(imgStr);
			if (m1.find()) {
				imgSrc = m1.group(1);
				if (imgSrc.indexOf("images.yamimap.com") > -1) {
					clinicManual.setContent(content.replaceAll(imgSrc, imgSrc + "?imageView2/2/q/100/interlace/1/w/" + width));
				}

			}
		}

		return clinicManual;
	}

	/**
	 * 提取富文本内容的图片地址
	 */
	public String getFirstImgStr(String htmlStr) {
		if (StringUtils.isBlank(htmlStr)) {
			return null;
		}
		List<String> pics = new ArrayList<>();
		String img = "";
		Pattern p_image;
		Matcher m_image;
		String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
		p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
		m_image = p_image.matcher(htmlStr);
		if (m_image.find()) {
			// 得到<img />数据
			img = m_image.group();
			// 匹配<img>中的src数据
			String reg = "src\\s*=\\s*\"?(.*?)(\"|>|\\s+)";
			final Pattern p1 = Pattern.compile(reg);
			Matcher m1 = p1.matcher(img);
			if (m1.find()) {
				return m1.group(1);
			}
		}
		return null;
	}

}
